//
//  HDWelcomeViewController.swift
//  HDSwfitWeibo
//
//  Created by wty on 16/4/11.
//  Copyright © 2016年 wty. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HDWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //加载之前的用户的数据
        downloadImage()
        prepareUI()
       //网络请求获取用户信息
      HDUserAccoutViewModel.userAccoutShare.loadHomeInfo { (error) -> () in
          //判断是否获取成功
        if error != nil{
            return
        }
        //成功加载图片
        self.downloadImage()
        }
    }
    //获取数据成功  进行异步下载图片
    private func  downloadImage() {
        //可选绑定
        if let urlString = HDUserAccoutViewModel.userAccoutShare.userAccount?.avatar_large{
            let url = NSURL(string: urlString)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named:"avatar_default_big" ))
        }
    }
    
    
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewDidAppear(animated)
        iconViewAnimation()
    }
    
    
    
    //添加子控件 并添加约束
    private func prepareUI ()  {
        //1.1.添加子控件
        view.addSubview(bgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        //2.0添加约束
        //2.1.禁用autosizing
        bgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        //2.2 添加背景约束
        bgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(view)
        }
        //2.3添加头像的约束
        iconView.snp_makeConstraints { (make) -> Void in
           make.centerX.equalTo(view)
           make.bottom.equalTo(view).offset(-160)
        }
        //2.4设置欢迎label约束
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }
    }
    //头部动画
    private func iconViewAnimation() {
        //动画修改头像的底部约束

            iconView.snp_updateConstraints { (make) -> Void in
                make.bottom.equalTo(view).offset(-(ScreenH-160) )
        }
                //Damping 弹簧效果
         UIView.animateWithDuration(animationCurtation , delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            //通知用户来重新布局
            self.view.layoutIfNeeded()
            }, completion: { (_) -> Void in
                //显示label
                UIView.animateWithDuration(animationCurtation, animations: { () -> Void in
                     self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        //回到主页
                        (UIApplication.sharedApplication().delegate as! AppDelegate ).switchViewController(true)
                        
                })
                
         })

    }
    
    
    
    //MARK -懒加载控件
    //1.背景
   private lazy var bgImageView:UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    //2.头像
    private lazy var iconView : UIImageView = {
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.layer.cornerRadius = 42.5
        iconView.layer.masksToBounds = true
        return iconView
    }()
    //3.欢迎label
    private lazy var welcomeLabel :UILabel = {
        let label = UILabel()
        //设置属性
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(HDWelcomeLabelFontSize)
        label.textColor = UIColor.blackColor()
//        label.alpha = 0
        label.sizeToFit()
        return label
    }()


}
